<template>
  <div class="requirementDetail-container">
    <div class="app-title-container title-position">
      <modules-title title-name="需求详情" style="margin-bottom: 20px;">
        <template v-if="detailsData" slot="btn">
          <el-button plain @click="goBack">返回</el-button>
          <el-button v-if="detailsData.demand_status==0" plain @click="handleDelete">删除</el-button>
          <el-button
            v-if="detailsData.demand_status==0"
            type="primary"
            @click="editRequirement"
          >编辑</el-button>
          <el-button
            v-if="detailsData.demand_status==0"
            type="primary"
            @click="handleSubmitAudit"
          >提交评估</el-button>
          <el-button
            v-hasPermi="{v:['demand:require:audit'], isF: detailsData.demand_status==1}"
            type="primary"
            class="$status-color4"
            @click="handleAudit"
          >
            审批</el-button>
          <el-button
            v-hasPermi="{v:['demand:require:evaluate'], isF: detailsData.demand_status==3}"
            type="primary"
            class="$status-color4"
            @click="evaluateRequirements"
          >
            评估需求</el-button>
          <el-button
            v-hasPermi="{v:['demand:require:evaluate'], isF: detailsData.demand_status==5}"
            type="primary"
            class="$status-color4"
            @click="reappraise"
          >
            重新评估</el-button>
          <el-button
            v-hasPermi="{v:['demand:project:add'], isF: detailsData.demand_status==6}"
            type="primary"
            class="$status-color4"
            @click="createProject"
          >
            创建自研立项</el-button>
        </template>
      </modules-title>
    </div>
    <div class="main-content-container">
      <div class="status-container">
        <status :status-data="statusData" />
      </div>
      <!-- <div v-if="detailsData&&detailsData.demand_status>3" class="pinggu-container">
        <evluationInfo :detail-data="detailsData" />
      </div> -->
      <div class="requirementInfo-container">
        <requirementInfo :requirement-detail="detailsData" :from="from" />
      </div>
      <div class="optionLog-container" style="margin-top:20px;">
        <optionLog ref="optionLog" :require-id="requireId" />
      </div>
    </div>
    <!-- 需求审批 -->
    <audit-dialog ref="auditDialog" :require-id="requireId" @auditSuccess="auditSuccess" />
    <!-- 需求评估 -->
    <evaluate-dialog ref="evaluateDialog" :require-id="requireId" :evaluate-type="evaluateType" @evaluateSuccess="evaluateSuccess" />
  </div>
</template>

<script>
import { requirementDetail, submitAudit, deleteRequire } from '@/api/requirement'
export default {
  name: 'RequirementDetail',
  components: {
    status: () => import('./modules/status'),
    requirementInfo: () => import('./modules/requirementInfo'),
    optionLog: () => import('./modules/optionLog'),
    AuditDialog: () => import('../modules/auditDialog'),
    EvaluateDialog: () => import('../modules/evaluateDialog.vue'),
    evluationInfo: () => import('./modules/evaluationInfo.vue')
  },
  data() {
    return {
      from: '', // 判断是否为从审批详情过来的
      requireId: '',
      evaluateType: 1,
      detailsData: null,
      statusData: {
        demand_status: 0
      }
    }
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {
    this.from = this.$route.query.from || ''
    this.requireId = this.$route.query.requireId
    if (this.requireId) {
      this.getRequirementDetail(this.requireId)
    }
  },
  methods: {
    // 编辑需求
    editRequirement() {
      this.$router.push({
        name: 'createRequirements',
        query: {
          types: 2,
          requireId: this.detailsData.id
        }
      })
    },
    handleAudit() {
      this.$refs.auditDialog.open()
    },
    auditSuccess() {
      this.getRequirementDetail(this.requireId)
    },
    // 评估需求
    evaluateRequirements() {
      this.$refs.evaluateDialog.open()
    },
    // 重新评估
    reappraise() {
      this.$refs.evaluateDialog.open()
    },
    evaluateSuccess() {
      this.currentId = ''
      this.getRequirementDetail(this.requireId)
      // 更新操作日志
      this.$refs.optionLog.afreshLogList()
    },
    // 草稿提交审核
    handleSubmitAudit() {
      submitAudit(this.detailsData.id).then((res) => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '提交成功'
          })
          this.getRequirementDetail(this.requireId)
          // 更新操作日志
          this.$refs.optionLog.afreshLogList()
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
    },
    // 返回之后刷新列表
    refreshBack() {
      this.$route.params.refresh = true
      this.$router.back()
    },
    goBack() {
      // this.$router.back()
      this.refreshBack()
    },
    getRequirementDetail(id) {
      requirementDetail(id).then((res) => {
        if (res.code === 200) {
          this.detailsData = res.data
          // 需求状态
          this.statusData.demand_status = this.detailsData.demand_status
          if (this.detailsData.demand_status === 3) {
            this.evaluateType = 1
          } else if (this.detailsData.demand_status === 5) {
            this.evaluateType = 2
          }
        }
      })
    },
    // 创建立项申请
    createProject() {
      this.$router.push({
        name: 'reqCreateEdit',
        query: {
          types: 1,
          requireId: this.requireId
        }
      })
    },
    handleDelete() {
      const that = this
      const datas = { id: this.requireId }
      this.$confirm(
        '是否确认删除该需求?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          deleteRequire(datas).then(res => {
            if (res.code === 200) {
              that.$message({
                type: 'success',
                message: '删除成功',
                duration: 1500,
                onClose: () => {
                  that.$route.params.refresh = true // 返回列表后更新列表，列表有缓存
                  that.$router.back()
                }
              })
            } else {
              that.$message({
                type: 'error',
                message: res.msg,
                duration: 1500
              })
            }
          })
        })
        .catch(function() {})
    }
  }
}
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */
.requirementDetail-container {
  padding-top: 110px;
  box-sizing: border-box;
}
</style>
